/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hudson.msservice.message_entry;

import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Hudson;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import hudson.msservice.messageservice.MessageServiceProxy;
import hudson.msservice.Messages;

/**
 *
 * @author janksenhu
 */
public class EmailMessageEntry {

    private String E_mail_subject = "";
    private String E_mail_content = "";

    public EmailMessageEntry() { }

    public String getE_mail_content() {
        return E_mail_content;
    }

    public void setE_mail_content(String E_mail_content) {
        this.E_mail_content = E_mail_content;
    }

    public String getE_mail_subject() {
        return E_mail_subject;
    }

    public void setE_mail_subject(String E_mail_subject) {
        this.E_mail_subject = E_mail_subject;
    }

    public EmailMessageEntry(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) {
        this.E_mail_subject = build.getJobName();
        this.E_mail_content = build.getResult().toString();
    }

    public void sendEmail(String receive,AbstractBuild<?, ?> build) {
        String from = "ptadmin@tencent.com";
        String to = "";
        if(receive.trim().length()==0){
//            receive=MysqlDb.getMessageReceiverList(build.getJobName(), build.getNumber());
        }
        for(String str:receive.split(";")){
            if(str.trim().length()!=0  && MessageUtil.isTencentDomainUser(str)){
                to+=str.trim()+"@tencent.com;";
            }
        }
        String cc = "";
        String bcc = to;
        String title = E_mail_subject;
//        String content = E_mail_content;
        String priority = MessageServiceProxy.MSG_PRIORITY_LOW;
//        String bodyFormat = MessageServiceProxy.MSG_MAIL_BODYFORMAT_TEXT;
        String bodyFormat = MessageServiceProxy.MSG_MAIL_BODYFORMAT_HTML;
        String emailType = MessageServiceProxy.MSG_MAIL_TYPE_ENCHANGE;
        List<String> attachFilePath = null;
        MessageServiceProxy instance = MessageServiceProxy.getInstance();
        
        Date dt =build.getTimestamp().getTime();
        SimpleDateFormat fm=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String tm= fm.format(dt);
        
        String html_content="</br>&nbsp;<span style='background-color:#73bae8;width:200px;font-size:20px;font-weight:600px'>"+Messages.Email_BaseInfor()+"</span></br><span style='font-size:13px;font-weight:300px'>&nbsp;&nbsp;"+Messages.Email_Jobname()+build.getJobName()+"</span></br><span style='font-size:13px;font-weight:300px'>&nbsp;&nbsp;"+Messages.Email_StartTime()+tm+"</span></br><span style='font-size:13px;font-weight:300px'>&nbsp;&nbsp;"+Messages.Email_Duration()+build.getDurationUntilNow()+"</span></br><span style='font-size:13px;font-weight:300px'>&nbsp;&nbsp;"+Messages.Email_BuildResult()+MessageUtil.getChineseOfResult(build.getResult().toString())+"</span><div /></br>&nbsp;<span style='background-color:#73bae8;width:200px;font-size:20px;font-weight:600px'>"+Messages.Email_AvailableURL()+"</span></br><span style='font-size:13px;font-weight:300px'>&nbsp;&nbsp;"+Messages.Email_LogContext()+"<a href='"+build.getConsoleTextUrlOfCurrentBuild()+"'>"+Messages.Email_CheckLog()+"</a></span></br><hr style='border-top:1px solid #73bae8;width:97%'/><span style='font-size:12px;font-weight:400px;color:gray'>&nbsp;"+Messages.Email_Left()+"&nbsp;<a href='"+Hudson.getInstance().getRootUrl()+"'>"+Messages.Email_Middle()+"</a>&nbsp;"+Messages.Email_Right()+"</span></br><span>&nbsp;&nbsp;&nbsp;</span>";
        String html_string="<div style='border-style:solid;border-width:5pt;border-color:#b9dbf4;background-color:#FFFFFF;margin-left:5px;margin-right:5px;height:350px;'><div style='background-color:#b9dbf4;'><font style='font-size:24px;font-weight:800px'><img src='"+Hudson.getInstance().getRootUrl()+"/images/logo.png' />&nbsp;</font></div>"+html_content+"</div>";
        instance.sendMail(from, to, cc, bcc, title, html_string, priority, bodyFormat, emailType, attachFilePath);
    }
    
    
    /**
     * send then tools's upstream project has not success , then would not shedule the job ,send email notice 
     */
    public void sendEmailNoticeUpstreamNotSuccess(String receive,AbstractBuild<?, ?> build){
        String from = "ptadmin@tencent.com";
        String to = "";
        if(receive.trim().length()==0){
//            receive=MysqlDb.getMessageReceiverList(build.getJobName(), build.getNumber());
        }
        for(String str:receive.split(";")){
            if(str.trim().length()!=0  && MessageUtil.isTencentDomainUser(str)){
                to+=str.trim()+"@tencent.com;";
            }
        }
        String cc = "";
        String bcc = to;
        String title = build.getFullDisplayName()+" tools schedule error";
//        String content = E_mail_content;
        String priority = MessageServiceProxy.MSG_PRIORITY_LOW;
//        String bodyFormat = MessageServiceProxy.MSG_MAIL_BODYFORMAT_TEXT;
        String bodyFormat = MessageServiceProxy.MSG_MAIL_BODYFORMAT_HTML;
        String emailType = MessageServiceProxy.MSG_MAIL_TYPE_ENCHANGE;
        List<String> attachFilePath = null;
        MessageServiceProxy instance = MessageServiceProxy.getInstance();
        
        Date dt =build.getTimestamp().getTime();
        SimpleDateFormat fm=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String tm= fm.format(dt);
        
        String html_content=build.getFullDisplayName()+" build result is: "+build.getResult().toString()+" could not schedule it's tools ";
        String html_string="<div style='border-style:solid;border-width:5pt;border-color:#b9dbf4;background-color:#FFFFFF;margin-left:5px;margin-right:5px;height:350px;'><div style='background-color:#b9dbf4;'><font style='font-size:24px;font-weight:800px'><img src='"+Hudson.getInstance().getRootUrl()+"/images/logo.png' />&nbsp;</font></div>"+html_content+"</div>";
        instance.sendMail(from, to, cc, bcc, title, html_string, priority, bodyFormat, emailType, attachFilePath);
    }
    
    /**
     * notice user that the required envrionment variable is empty ,could not build 
     */
     public static void sendEmailNotifyThatRequiredEnvIsEmpty(String receive,AbstractBuild<?, ?> build,List<String> emptylist){
        String from = "ptadmin@tencent.com";
        String to = "";
        if(receive.trim().length()==0){
//            receive=MysqlDb.getMessageReceiverList(build.getJobName(), build.getNumber());
        }
        for(String str:receive.split(";")){
            if(str.trim().length()!=0  && MessageUtil.isTencentDomainUser(str)){
                to+=str.trim()+"@tencent.com;";
            }
        }
        String cc = "";
        String bcc = to;
        String title = build.getFullDisplayName()+" build error";
        String priority = MessageServiceProxy.MSG_PRIORITY_LOW;
        String bodyFormat = MessageServiceProxy.MSG_MAIL_BODYFORMAT_HTML;
        String emailType = MessageServiceProxy.MSG_MAIL_TYPE_ENCHANGE;
        List<String> attachFilePath = null;
        MessageServiceProxy instance = MessageServiceProxy.getInstance();
        
        Date dt =build.getTimestamp().getTime();
        SimpleDateFormat fm=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String tm= fm.format(dt);
        
        String html_content=build.getFullDisplayName()+" build exit beacuse those global variable is empty: "+emptylist;
        String html_string="<div style='border-style:solid;border-width:5pt;border-color:#b9dbf4;background-color:#FFFFFF;margin-left:5px;margin-right:5px;height:350px;'><div style='background-color:#b9dbf4;'><font style='font-size:24px;font-weight:800px'><img src='"+Hudson.getInstance().getRootUrl()+"/images/logo.png' />&nbsp;</font></div>"+html_content+"</div>";
        instance.sendMail(from, to, cc, bcc, title, html_string, priority, bodyFormat, emailType, attachFilePath);
     }
}  
